jQuery(function() {
    var i_nghi_nv = parseInt(jQuery('#total-nghi-nv').val());
    var i_phat_nv = parseInt(jQuery('#total-phat-nv').val());
    var i_phat_to = parseInt(jQuery('#total-phat-to').val());
    jQuery('#ghi-chu').keyup(function(){
        var length = jQuery(this).val().length;
        jQuery(this).parent().find('#ghi-chu-length').text(length);
    });
    jQuery('#ghi-chu').keyup();
    jQuery('.field-remove-submit').click(function(){
        jQuery(this).parent().parent().remove();
    });
    jQuery('#scl-ket-qua').change(function(){
        var ket_qua = jQuery(this).val();
        if (ket_qua != 2) {
            jQuery('.thuc-te-wrapper').show();
        }
        else {
            jQuery('.thuc-te-wrapper').hide();
            jQuery('#txt-thuc-te').val(jQuery('#ct-data-text').text());
        }
    });
    jQuery('#scl-ket-qua').change();
    jQuery('.format_currency').keyup(function(){
        var val = currencyToNumber(jQuery(this).val());
        if (isNaN(val)) {
            jQuery(this).val('');
            val = 0;
        }
        else {
            jQuery(this).val(numberToCurrency(val));
        }
    });
    jQuery('#add-more-nghi-nv').click(function(){
        i_nghi_nv++;
        var html = '<div class="form-item-row">' +
                        '<div class="form-item form-text-wrapper">' +
                          '<label for="txt-nghi-nv-'+i_nghi_nv+'">Nhân viên</label>' +
                          '<input type="text" class="form-text form-autocomplete text txt-nghi-nv" autocomplete="OFF" name="nghi_nv[]" value="" id="txt-nghi-nv-'+i_nghi_nv+'">' +
                        '</div>' +
                        '<div class="form-item">' +
                          '<input type="button" class="field-remove-submit form-submit" value="X" id="field-remove-nghi-nv-submit-'+i_nghi_nv+'">' +
                        '</div>' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="scl-thoi-luong-nghi-'+i_nghi_nv+'">Thời gian</label>' +
                          '<select class="form-select select scl-thoi-luong-nghi" name="thoi_luong_nghi[]" id="scl-thoi-luong-nghi-'+i_nghi_nv+'">' +
                            '<option selected="selected" value="1">Cả ngày</option>' +
                            '<option value="0.5">Nửa ngày</option>' +
                          '</select>' +
                        '</div>' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="scl-ly-do-nghi-'+i_nghi_nv+'">Lý do</label>' +
                          '<select class="form-select select scl-ly-do-nghi" name="ly_do_nghi[]" id="scl-ly-do-nghi-'+i_nghi_nv+'">' +
                            '<option selected="selected" value="Nghỉ phép năm">Nghỉ phép năm</option>' +
                            '<option value="Nghỉ ốm">Nghỉ ốm</option>' +
                            '<option value="Nghỉ thai sản">Nghỉ thai sản</option>' +
                            '<option value="Nghỉ không phép">Nghỉ không phép</option>' +
                          '</select>' +
                        '</div>' +
                        '<div class="clear"></div>' +
                      '</div>';
        jQuery('#form-item-nghi-nv-wrapper').append(html);
        var input = jQuery('#txt-nghi-nv-' + i_nghi_nv)
            .attr('autocomplete', 'OFF')
            .attr('aria-autocomplete', 'list');
        jQuery(input[0].form).submit(Drupal.autocompleteSubmit);
        input.parent()
            .attr('role', 'application')
            .append(jQuery('<span class="element-invisible" aria-live="assertive"></span>')
              .attr('id', input.attr('id') + '-autocomplete-aria-live')
            );
        var auto_path = new Drupal.ACDB(jQuery('#txt-nghi-nv-1-autocomplete').val());
        new Drupal.jsAC(input, auto_path);
        jQuery('#field-remove-nghi-nv-submit-' + i_nghi_nv).click(function(){
            jQuery(this).parent().parent().remove();
        });
    });
    jQuery('#add-more-phat-nv').click(function(){
        i_phat_nv++;
        var html = '<div class="form-item-row">' +
                        '<div class="form-item form-text-wrapper">' +
                          '<label for="txt-phat-nv-'+i_phat_nv+'">Nhân viên</label>' +
                          '<input type="text" class="form-text form-autocomplete text txt-phat-nv" autocomplete="OFF" name="phat_nv[]" value="" id="txt-phat-nv-'+i_phat_nv+'">' +
                        '</div>' +
                        '<div class="form-item">' +
                          '<input type="button" class="field-remove-submit form-submit" value="X" id="field-remove-phat-nv-submit-'+i_phat_nv+'">' +
                        '</div>' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="txt-tien-phat-nv-'+i_phat_nv+'">Tiền phạt</label>' +
                            '<input type="text" class="format_currency form-text text txt-tien-phat-nv" autocomplete="OFF" name="tien_phat_nv[]" value="" id="txt-tien-phat-nv-'+i_phat_nv+'">' +
                        '</div>' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="txt-ly-do-phat-nv-'+i_phat_nv+'">Lý do</label>' +
                            '<input type="text" class="form-text text txt-ly-do-phat-nv" autocomplete="OFF" name="ly_do_phat_nv[]" value="" id="txt-ly-do-phat-nv-'+i_phat_nv+'">' +
                        '</div>' +
                        '<div class="clear"></div>' +
                      '</div>';
        jQuery('#form-item-phat-nv-wrapper').append(html);
        var input = jQuery('#txt-phat-nv-' + i_phat_nv)
            .attr('autocomplete', 'OFF')
            .attr('aria-autocomplete', 'list');
        jQuery(input[0].form).submit(Drupal.autocompleteSubmit);
        input.parent()
            .attr('role', 'application')
            .append(jQuery('<span class="element-invisible" aria-live="assertive"></span>')
              .attr('id', input.attr('id') + '-autocomplete-aria-live')
            );
        var auto_path = new Drupal.ACDB(jQuery('#txt-phat-nv-1-autocomplete').val());
        new Drupal.jsAC(input, auto_path);
        jQuery('#field-remove-phat-nv-submit-' + i_phat_nv).click(function(){
            jQuery(this).parent().parent().remove();
        });
        jQuery('#txt-tien-phat-nv-'+i_phat_nv).keyup(function(){
            var val = currencyToNumber(jQuery(this).val());
            if (isNaN(val)) {
                jQuery(this).val('');
                val = 0;
            }
            else {
                jQuery(this).val(numberToCurrency(val));
            }
        });
    });
    jQuery('#add-more-phat-to').click(function(){
        i_phat_to++;
        var html = '<div class="form-item-row">' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="txt-tien-phat-to-'+i_phat_to+'">Tiền phạt</label>' +
                            '<input type="text" class="format_currency form-text text txt-tien-phat-to" autocomplete="OFF" name="tien_phat_to[]" value="" id="txt-tien-phat-to-'+i_phat_to+'">' +
                        '</div>' +
                        '<div class="form-item">' +
                          '<input type="button" class="field-remove-submit form-submit" value="X" id="field-remove-phat-to-submit-'+i_phat_to+'">' +
                        '</div>' +
                        '<div class="form-item form-select-wrapper">' +
                          '<label for="txt-ly-do-phat-to-'+i_phat_to+'">Lý do</label>' +
                            '<input type="text" class="form-text text txt-ly-do-phat-to" autocomplete="OFF" name="ly_do_phat_to[]" value="" id="txt-ly-do-phat-to-'+i_phat_to+'">' +
                        '</div>' +
                        '<div class="clear"></div>' +
                      '</div>';
        jQuery('#form-item-phat-to-wrapper').append(html);
        jQuery('#field-remove-phat-to-submit-' + i_phat_to).click(function(){
            jQuery(this).parent().parent().remove();
        });
        jQuery('#txt-tien-phat-to-'+i_phat_to).keyup(function(){
            var val = currencyToNumber(jQuery(this).val());
            if (isNaN(val)) {
                jQuery(this).val('');
                val = 0;
            }
            else {
                jQuery(this).val(numberToCurrency(val));
            }
        });
    });
});

function check_nhap_ket_qua() {
    if (confirm('Sau khi nhập bạn sẽ không thể sửa dữ liệu!\n\rBạn có chắc đã nhập đúng kết quả sản xuất ?')) {
//        jQuery('#submit').click();
        return true;
    }
    return false;
}